package czk.concrent.threadstatus.practice;

import sun.awt.windows.ThemeReader;

import java.util.concurrent.TimeUnit;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/8/7 23:49
 */
public class DemonThread {
    public static void main(String[] args) throws InterruptedException {
        ThreadDemo threadDemo=new ThreadDemo();
        // 开启t1
        threadDemo.startThread1();
        // 开启t2 并设置t1为t2的守护线程
        threadDemo.startThread2();
        Thread.sleep(10*1000);
        //threadDemo.stopT2();
    }
}
class ThreadDemo{
    private Thread t1;
    private Thread t2;

    /**
     * 设置线程一
     */
    public void startThread1() throws InterruptedException {
        t1=new Thread(()->{
           while (true){
               System.out.println(123);
               try {
                   Thread.sleep(2*1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        });
        Thread.sleep(100);

    }

    /**
     * 设置线程t2
     */
    public void startThread2(){
        t2=new Thread(()->{
            // 必须县设值守护线程在启动
            t1.setDaemon(true);
            t1.start();
            for (int i = 0; i < 5; i++) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t2.start();
    }
    public void stopT2(){
        t2.interrupt();
    }
}